/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ModeratorOperations;


import CRUD.*;
import java.util.Iterator;
import java.util.Vector;
import java.util.Set;
import xmlMapping.Forum;
import xmlMapping.Thread;
import xmlMapping.Post;
/**
 *
 * @author Mostafa
 */
public class ModeratorManageThreads {
   
    public void mergeThreads(String title,Vector<Integer> threads_id)
    {
        
        Integer firstid=threads_id.get(0);
   
   ThreadHome th= new ThreadHome();
   PostHome ph=new PostHome();
    Thread first = th.findById(threads_id.get(0));
    first.setTitle(title);
    first.setBlocked(false);
    first.setSticky(false);
    first.setNoOfVisitor(0);
    first.setLocked(false);
        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        first.setLastModified(sqlDate);
     Thread merge=new Thread(first.getMember(), first.getForum(), title, first.getBody(), false ,false , false,0,  sqlDate);
  
     //Integer lastpost=null;
    // if(first.getPost()!=null)
     //{lastpost=first.getPost().getPostId();}
       
     
     
     
     th.session.close();
     
        for (int i = 1; i < threads_id.size(); i++) {
            th=new ThreadHome();
           Thread currentthread=th.findById(threads_id.get(i));
           first.setBody(first.getBody()+ currentthread.getBody());
           Set<Post> currentposts= currentthread.getPosts();
           Iterator<Post> itr=currentposts.iterator();
           th.session.close();
            while (itr.hasNext()) {
                Post post = itr.next();
                post.setThread(first);
                ph.attachDirty(post);
               // ph.releaseConnection();
            }
            
            new ThreadHome().delete(currentthread);
            
        }
     
     new ThreadHome().attachDirty(first);
    /*************************
     //if(first.getPost())
     
     
     ////////////////////
       Forum ff=  first.getForum();
          Post lastPost=null;
       if(ff.getPost()!=null)
       {  lastPost=ff.getPost();}
      //  ff.getPost().getThread();
        
     ////////////////////
     
        int numberofposts=0;
        
          Set<Post> mergeposts=new HashSet<Post>();
          
          numberofposts+=first.getPosts().size();
          Iterator<Post> fitr=first.getPosts().iterator();
         while (fitr.hasNext()) {
            Post post = fitr.next(); 
           Post p=new Post(post.getMember(), merge, post.getTitle(), post.getBody(), post.getLastModified(), post.getRank()) ;
           if(lastPost!=null)
           {
               if( !post.getPostId().equals(lastPost.getPostId()))
             {
                 mergeposts.add(p);
             }
           }
             
        }
          
          
          
          
          
        for (int i = 1; i < threads_id.size(); i++) {
           
            Thread t= th.findById(threads_id.get(i));
       
          Iterator<Post> itr2 = t.getPosts().iterator();
         
        Forum currentforum=t.getForum();
        lastPost=currentforum.getPost();
        
          numberofposts+=t.getPosts().size();
          
            while (itr2.hasNext()) {
                Post post = itr2.next();
                Post p=new Post(post.getMember(), merge, post.getTitle(), post.getBody(), post.getLastModified(), post.getRank()) ;
                if(lastPost!=null)
                {
              if(! post.getPostId().equals(lastPost.getPostId()))
             {
           mergeposts.add(p);
             }
                
                }
                   
            }
             merge.setBody(merge.getBody()+t.getBody());
        
        }
        // new ThreadHome().delete(first); 
     
        merge.setPosts(mergeposts);
        merge.setNoofpost(numberofposts);
        th.persist(merge);
        
        
          Iterator<Post> itr3 =mergeposts.iterator();
        while (itr3.hasNext()) {
            Post post = itr3.next();
            ph.persist(post);
             System.out.println(post.getTitle()+"   thread"+post.getThread().getTitle());
        }
        
        Post forumlastpost=null;
         
          Forum myf =  null;
        for (int i = 0; i < threads_id.size(); i++) {
            Thread currentthread=th.findById(threads_id.get(0));
            if(currentthread!=null)
            myf=currentthread.getForum();
            if(myf.getPost()!=null)
            {  Post post=ph.findById(myf.getPost().getPostId());
            
            if( post.getThread().getThreadId().equals(currentthread.getThreadId()))
            {
                 Post p=new Post(post.getMember(), merge, post.getTitle(), post.getBody(), post.getLastModified(), post.getRank()) ;
                 ph.persist(p);
                 forumlastpost= p;
                
            }
            
            
            }  
                 
              th.delete(th.findById(threads_id.get(i)));
           
        }
        
        //th.session.close();
        if(forumlastpost!=null)
        {
          ForumHome home=new ForumHome();
          myf.setPost(lastPost);
          home.attachDirty(myf);
        }
        */
    }

    
    
    public void splitThread(Thread oldthread,Thread newthread,String[] newthreadpostsids)
    {
        PostHome ph=new PostHome();
        ThreadHome th=new ThreadHome();
        Thread ot=th.findById(oldthread.getThreadId());
        ot.setNoofpost(oldthread.getNoofpost()-newthreadpostsids.length);
        ot.setBody(oldthread.getBody());
        ot.setTitle(oldthread.getTitle());
        ot.setThread_description(oldthread.getThread_description());
        th.attachDirty(ot);
        newthread.setNoofpost(newthreadpostsids.length);
        th.persist(newthread);
        
        for (int i = 0; i < newthreadpostsids.length; i++) {
            Post p=ph.findById(Integer.parseInt(newthreadpostsids[i]));
            p.setThread(newthread);
            ph.attachDirty(p);
        }
           
    
    
    }
    
        
    
    public void editThread(Thread Editthread)
    {
    
    new ThreadHome().attachDirty(Editthread);
    }
   
    
    public void moveThread(Thread movethread)
    {
        new ThreadHome().merge(movethread);
    }
  
    
    
    public void stick(Thread stichthread)
    {
    
        new ThreadHome().attachDirty(stichthread);
    
    }
    
   public void block(Thread stichthread)
    {
        stichthread.setBlocked(true);
        new ThreadHome().attachDirty(stichthread);
    
    }
    public void unblock(Thread stichthread)
    {
          stichthread.setBlocked(false);
        new ThreadHome().attachDirty(stichthread);
    
    }
    
    
    public void deleteThreads(Vector<Integer> ids)
    {
        ThreadHome th=new ThreadHome();
        ForumHome fh=new ForumHome();
        Integer deletedpostno=0;
         Thread firstthread=th.findById(ids.get(0));
         Forum f= fh.findById(firstthread.getForum().getForumId());
           
        for (int i = 0; i < ids.size(); i++) {
            Thread c=th.findById(ids.get(i));
            if(f.getPost()!=null)
            {
            if(f.getPost().getThread().getThreadId().equals(ids.get(i)) )
            {
            f.setPost(null);
            fh.attachDirty(f);
            }
            }
            deletedpostno+= c.getPosts().size();
            th.delete(c);
        }
        f.setNoofpost(f.getNoofpost()-deletedpostno);
        f.setNoofthread(f.getNoofthread()-ids.size());
        fh.attachDirty(f);
    }
    
   
    
}
